package tech.dongling.boot.plugins.impl;

import com.google.common.util.concurrent.RateLimiter;
import tech.dongling.boot.plugins.Limiter;

import java.util.Properties;

// Guava限流插件
public class GuavaLimiter implements Limiter {
    private RateLimiter rateLimiter;

    @Override
    public void init(Properties config) {
        if ("true".equals(config.getProperty("limiter.enabled"))) {
            double qps = Double.parseDouble(config.getProperty("limiter.qps", "1000"));
            rateLimiter = RateLimiter.create(qps);
        }
    }

    @Override
    public boolean tryAcquire() {
        return rateLimiter.tryAcquire();
    }
}